package org.code.boot.service.common.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.code.boot.common.utils.DateUtils;
import org.code.boot.common.utils.SecurityUtils;
import org.code.boot.common.utils.ServletUtils;
import org.code.boot.common.utils.ip.IpUtils;
import org.code.boot.service.system.ISysConfigService;
import org.code.boot.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.code.boot.mapper.common.ComLikeMapper;
import org.code.boot.domain.common.ComLike;
import org.code.boot.service.common.IComLikeService;

/**
 * 通用点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-27
 */
@Service
public class ComLikeServiceImpl implements IComLikeService
{
    @Autowired
    private ComLikeMapper comLikeMapper;
    
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询通用点赞
     * 
     * @param likeId 通用点赞主键
     * @return 通用点赞
     */
    @Override
    public ComLike selectBizLikeByLikeId(Long likeId)
    {
        return comLikeMapper.selectBizLikeByLikeId(likeId);
    }

    /**
     * 查询通用点赞列表
     * 
     * @param comLike 通用点赞
     * @return 通用点赞
     */
    @Override
    public List<ComLike> selectBizLikeList(ComLike comLike)
    {
        return comLikeMapper.selectBizLikeList(comLike);
    }

    /**
     * 新增通用点赞
     * 
     * @param comLike 通用点赞
     * @return 结果
     */
    @Override
    public int insertBizLike(ComLike comLike)
    {
        comLike.setCreateTime(DateUtils.getNowDate());
        return comLikeMapper.insertBizLike(comLike);
    }

    /**
     * 修改通用点赞
     * 
     * @param comLike 通用点赞
     * @return 结果
     */
    @Override
    public int updateBizLike(ComLike comLike)
    {
        comLike.setUpdateTime(DateUtils.getNowDate());
        return comLikeMapper.updateBizLike(comLike);
    }

    /**
     * 批量删除通用点赞
     * 
     * @param likeIds 需要删除的通用点赞主键
     * @return 结果
     */
    @Override
    public int deleteBizLikeByLikeIds(Long[] likeIds)
    {
        return comLikeMapper.deleteBizLikeByLikeIds(likeIds);
    }

    /**
     * 删除通用点赞信息
     * 
     * @param likeId 通用点赞主键
     * @return 结果
     */
    @Override
    public int deleteBizLikeByLikeId(Long likeId)
    {
        return comLikeMapper.deleteBizLikeByLikeId(likeId);
    }

    /**
     * 点赞/取消点赞
     * 
     * @param businessType 业务类型
     * @param businessId 业务ID
     * @return 结果
     */
    @Override
    public AjaxResult toggleLike(String businessType, Long businessId)
    {
        // 检查业务类型是否允许点赞
        String allowTypes = configService.selectConfigByKey("biz.like.business.types");
        if (!allowTypes.contains(businessType)) {
            return AjaxResult.error("该业务类型不支持点赞功能");
        }

        // 获取用户信息
        Long userId = null;
        String userIp = IpUtils.getIpAddr(ServletUtils.getRequest());
        
        try {
            userId = SecurityUtils.getUserId();
        } catch (Exception e) {
            // 未登录用户
            String allowAnonymous = configService.selectConfigByKey("biz.like.allow.anonymous");
            if (!"true".equals(allowAnonymous)) {
                return AjaxResult.error("请先登录");
            }
            userId = 0L;
        }

        // 检查是否已点赞
        ComLike existLike = comLikeMapper.checkLikeExists(businessType, businessId, userId, userIp);
        
        Map<String, Object> result = new HashMap<>();
        
        if (existLike != null) {
            // 取消点赞
            comLikeMapper.cancelLike(businessType, businessId, userId, userIp);
            result.put("liked", false);
            result.put("message", "取消点赞成功");
        } else {
            // 添加点赞
            ComLike comLike = new ComLike();
            comLike.setBusinessType(businessType);
            comLike.setBusinessId(businessId);
            comLike.setUserId(userId);
            comLike.setUserIp(userIp);
            comLike.setCreateBy(userId > 0 ? SecurityUtils.getUsername() : "anonymous");
            comLike.setCreateTime(DateUtils.getNowDate());
            
            comLikeMapper.insertBizLike(comLike);
            result.put("liked", true);
            result.put("message", "点赞成功");
        }
        
        // 获取最新点赞数
        int likeCount = comLikeMapper.countLikes(businessType, businessId);
        result.put("likeCount", likeCount);
        
        return AjaxResult.success(result);
    }

    /**
     * 获取点赞数和点赞状态
     * 
     * @param businessType 业务类型
     * @param businessId 业务ID
     * @return 结果
     */
    @Override
    public AjaxResult getLikeInfo(String businessType, Long businessId)
    {
        Map<String, Object> result = new HashMap<>();
        
        // 获取点赞数
        int likeCount = comLikeMapper.countLikes(businessType, businessId);
        result.put("likeCount", likeCount);
        
        // 获取当前用户点赞状态
        Long userId = null;
        String userIp = IpUtils.getIpAddr(ServletUtils.getRequest());
        boolean liked = false;
        
        try {
            userId = SecurityUtils.getUserId();
        } catch (Exception e) {
            userId = 0L;
        }
        
        ComLike existLike = comLikeMapper.checkLikeExists(businessType, businessId, userId, userIp);
        if (existLike != null) {
            liked = true;
        }
        
        result.put("liked", liked);
        
        return AjaxResult.success(result);
    }
}